package cn.stylefeng.guns.modular.school.service.impl;

import cn.stylefeng.guns.base.consts.ConstantsContext;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.school.entity.TeacherInfo;
import cn.stylefeng.guns.modular.school.mapper.TeacherInfoMapper;
import cn.stylefeng.guns.modular.school.model.params.TeacherInfoParam;
import cn.stylefeng.guns.modular.school.model.result.TeacherInfoResult;
import cn.stylefeng.guns.modular.school.service.TeacherInfoService;
import cn.stylefeng.guns.sys.core.constant.Const;
import cn.stylefeng.guns.sys.modular.system.entity.Role;
import cn.stylefeng.guns.sys.modular.system.model.UserDto;
import cn.stylefeng.guns.sys.modular.system.service.RoleService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 教师信息 服务实现类
 * </p>
 *
 * @author
 * @since 2019-11-04
 */
@Service
public class TeacherInfoServiceImpl extends ServiceImpl<TeacherInfoMapper, TeacherInfo> implements TeacherInfoService {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @Override
    @Transactional(rollbackOn = Exception.class)
    public void add(TeacherInfoParam param) {
        TeacherInfo entity = getEntity(param);
        this.save(entity);

        UserDto userDto = new UserDto();
        userDto.setAccount(param.getCode());
        String defaultPassword = ConstantsContext.getDefaultPassword();
        userDto.setPassword(defaultPassword);
        userDto.setBirthday(param.getBirthday());
        userDto.setName(param.getName());
        userDto.setPhone(param.getTel());
        userDto.setSex(param.getSex());
        Role role = roleService.findByDescription(Const.CLASS_TEACHER);
        userDto.setRoleId(role.getRoleId().toString());
        userService.addUser(userDto);
    }

    @Override
    public void delete(TeacherInfoParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(TeacherInfoParam param) {
        TeacherInfo oldEntity = getOldEntity(param);
        TeacherInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public TeacherInfoResult findBySpec(TeacherInfoParam param) {
        QueryWrapper<TeacherInfo> queryWrapper = new QueryWrapper<>();
        if (ToolUtil.isNotEmpty(param.getCode())) {
            queryWrapper.eq("code", param.getCode());
        }
        TeacherInfo teacherInfo = this.getOne(queryWrapper);
        if (teacherInfo == null) {
            return null;
        }
        TeacherInfoResult teacherInfoResult = new TeacherInfoResult();
        ToolUtil.copyProperties(teacherInfo, teacherInfoResult);
        return teacherInfoResult;
    }

    @Override
    public List<TeacherInfoResult> findListBySpec(TeacherInfoParam param) {
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(TeacherInfoParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public TeacherInfo findByCode(String code) {
        return this.getOne(new QueryWrapper<TeacherInfo>().eq("code",code));
    }

    private Serializable getKey(TeacherInfoParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private TeacherInfo getOldEntity(TeacherInfoParam param) {
        return this.getById(getKey(param));
    }

    private TeacherInfo getEntity(TeacherInfoParam param) {
        TeacherInfo entity = new TeacherInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
